package com.zjh.staticFiles.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zjh.common.entity.Response;
import com.zjh.staticFiles.entity.StaticFileCategory;
import com.zjh.staticFiles.entity.StaticFilesLib;
import com.zjh.staticFiles.service.IStaticFilesLibService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-08-26
 */
@RestController
@RequestMapping("/staticFiles/static-files-lib")
public class StaticFilesLibController {
    @Resource
    private IStaticFilesLibService staticFilesLibService;

    @GetMapping("getAll")
    public Response<?> getAll() {
        List<StaticFilesLib> list = staticFilesLibService.list();

        return Response.success(list);
    }

    @PostMapping("getAllByCategory")
    public Response<?> getAllByCategory(@RequestBody StaticFileCategory category) {
        QueryWrapper<StaticFilesLib> queryWrapper = new QueryWrapper<>();

        if (category != null) {
            if (category.getId() != 0) {
                queryWrapper.eq("category_id", category.getId());
            }
        }

        List<StaticFilesLib> list = staticFilesLibService.list(queryWrapper);
        List<StaticFilesLib> res = list.stream().sorted(Comparator.comparing(StaticFilesLib::getCategoryId))
                .toList();

        return Response.success(res);
    }
}

